package com.guowei.jiecloudbackstage.common.api.vo;

import lombok.Data;

import java.io.Serializable;

/**
 * api 统一返回数据实体类  前后端分离的情况
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    private boolean success = true;

    /**
     * 返回处理消息
     */
    private String msg = "操作成功！";

    /**
     * 返回代码
     */
    private Integer code = 0;

    private Long count=0L;

    /**
     * 额外信息,显示统计信息等
     */
    private String statisticInfo = "";

    /**
     * 返回数据对象 data
     */
    private T data;

    public Result() {

    }

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    public void error500(String message) {
        this.msg = message;
        this.code = 500;
        this.success = false;
    }

    public void success(String message) {
        this.msg = message;
        this.code = 0;
        this.success = true;
    }

    public static Result<Object> error(String msg) {
        return error(500, msg);
    }

    public static Result<Object> error(int code, String msg) {
        Result<Object> r = new Result<Object>();
        r.setCode(code);
        r.setMsg(msg);
        r.setSuccess(false);
        return r;
    }

    public static Result<Object> ok(String msg) {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(0);
        r.setMsg(msg);
        return r;
    }

    public static Result<Object> ok(Object data) {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(200);
        r.setData(data);
        return r;
    }

    public static Result<Object> ok(int code,Object data) {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(code);
        r.setData(data);
        return r;
    }
}
